package com.igeek.boot.boot.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.domain.geo.GeoLocation;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @package com.igeek.boot.boot.service
 * @Description 位置标记业务类
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/19 18:20
 */
@Service
@Slf4j
public class LocationService {

    @Autowired
    private RedisTemplate  redisTemplate;

    //geo的key
    private static final String LOCATION_KEY = "gaode::location";
    //geo的member
    private static final String LOCATION_MEMBER = "member::";
    //将数据存入redis中
    public boolean saveLocation(String longitude, String latitude,String cityCode) {
        //使用redis中的geo存入
        //1.先进行查询redis中是否存在该地理位置
        List<String> members = redisTemplate.opsForGeo().position(LOCATION_KEY, LOCATION_MEMBER + cityCode);
        if (members.get(0)!= null && members.size() > 0) {
            log.info("该地理位置已经存在");
            return false;
        }
        Long add = redisTemplate.opsForGeo().add(LOCATION_KEY, new Point(Double.parseDouble(longitude), Double.parseDouble(latitude)), LOCATION_MEMBER + cityCode);
        log.info("add:{}",add);
        if (add == 1) {
            return true;
        } else {
            return false;
        }
    }

    //删除坐标信息
    public boolean deleteLocation(String cityCode) {
        Long remove = redisTemplate.opsForGeo().remove(LOCATION_KEY, LOCATION_MEMBER + cityCode);
        log.info("remove:{}",remove);
        if (remove == 1) {
            return true;
        } else {
            return false;
        }
    }

    //查询坐标信息
    public String getLocation(String cityCode) {
        //使用redis中的geo查询
        List position = redisTemplate.opsForGeo().position(LOCATION_KEY, LOCATION_MEMBER + cityCode);
        if (position == null) {
            return null;
        };
        Point point = (Point) position.get(0);
        log.info("point:{}",point);
        if (point!= null) {
            return point.getX() + "," + point.getY();
        } else {
            return null;
        }
    }

    //查询redis中LOCATION_KEY的所有member
    public List<String> getLocationMembers() {

        Circle circle = new Circle(new Point(114.309091, 30.600064), new Distance(10000, Metrics.KILOMETERS));
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
                .newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates()
                .sortAscending()
                .limit(100);

        GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo().radius(LOCATION_KEY, circle, args);
        log.info("results:{}",results);
        //获取results中的point属性
        List<String> collect = results.getContent().stream().map(geoLocation -> {
            //name为member::cityCode  只获取cityCode
            return geoLocation.getContent().getName().substring(LOCATION_MEMBER.length());
        }).collect(Collectors.toList());
        log.info("collect:{}",collect);
        return collect;
    }
}
